﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IESN.DM;
using IESN.Tools;
using System.Collections;

namespace IESN.BusinessCore
{
    public class GlobalCache
    {
        #region 单例

        private static object _lockObj = new object();
        private static GlobalCache _Instance;
        public static GlobalCache GetInstance
        {
            get
            {
                lock (_lockObj)
                {
                    if (_Instance == null)
                    {
                        _Instance = new GlobalCache();
                    }

                    return _Instance;
                }
            }
        }

        private GlobalCache()
        {
        }

        #endregion 
          
        private static object _lockDm = new object();
         
        private Dictionary<string, DmClass> dmMapping = new Dictionary<string, DmClass>();

        public Dictionary<string, DmClass> DmMapping
        {
            get { return dmMapping; }
            set { dmMapping = value; }
        }

        #region DM Mapping

        public short AddDmObject(DmClass dmObject)
        {
            lock (_lockDm)
            {
                short aIndex = -3;
                try
                { 
                    dmMapping[dmObject.DMObjectIndexName] = dmObject;
                }
                catch (Exception exception1)
                {
                    LogHelper.Instance.Error(exception1.Message, exception1);
                }

                return aIndex;
            }
        }

        public DmClass GetDmObject(string theDMObjectIndexName)
        {
            lock (_lockDm)
            {
                DmClass aDmItem = null;
                try
                {
                    if (dmMapping.ContainsKey(theDMObjectIndexName))
                    {
                        aDmItem = dmMapping[theDMObjectIndexName];
                    }
                }
                catch (Exception exception1)
                {
                    LogHelper.Instance.Error(exception1.Message, exception1);
                }

                return aDmItem;
            }
        }

        #endregion 

    }
}
